home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: What is &Variable (declared as: char Variable[10])?
- Date: 27 Feb 1996 15:32:34 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Feb27083234@qcd.lanl.gov>
- References: <4gqpa1$3h9@alcor.usc.edu> <4gsdno$1bg@umbc9.umbc.edu>
- <4gtab6$acb@ceylon.gte.com>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: Brenda's message of 26 Feb 1996 21:56:54 GMT
-
- In article <4gtab6$acb@ceylon.gte.com>
- Brenda <g051286> writes:
- <snip>
- B: >|> char myarray[10];
- <snip>
- B: >Yes it could and yes it is...'myarray' is not a pointer, but &myarray is
- B: >a pointer to 'myarray'.
- B:
- B: Um, that's not correct. myarray is DEFINITELY a pointer! As
- declared above,
- B: it is a constant pointer to 10 contiguous char datatypes. myarray is an
-
- Don't fill this newsgroup with garbage, we don't need this discussion
- once again. Please chastise your service provider in the strongest
- possible terms: it is common decency on their part not to flood proper
- discussions with newbies who have no idea of nettiquette.
-
- Most newsgroups have FAQs (Frequently Asked Questions with their
- answers), and most are available by anonymous ftp from
- rtfm.mit.edu. It is considered proper to retrieve them and read them
- before posting, so as not to waste everybody's time with very common
- misunderstandings. If you read and understand what the FAQ is saying,
- and you think it is wrong, and you have enough reason to believe so,
- that point is certainly welcome. Bringing it up without reading the
- FAQ gets no one's sympathy.
-
- In any case, it is polite to listen to a conversation a while before
- jumping in. The same is true of USENET: and I am sure if you had
- listened patiently for a week or two, you would have seen why you are
- mistaken.
-
- In any case, an array in C is not a pointer, just as `int i' does not
- declare a pointer. `myarray' above and `i' in my declaration both
- declare a region of storage, one for a sequence of 10 chars, and the
- other for one int. `int *ptr', on the other hand, reserves storage for
- a pointer to int. Note that `myarray' is not associated with any
- storage for a pointer, and `ptr' is not associated with any storage
- for an int. This can be easily verified by applying the `sizeof'
- operator to each of them. One can easily verify as well that `char
- myarray[10]' followed by `extern char *myarray' is a recipe for
- disaster.
-
- In most occurances of an object in an expression (for example when one
- is not taking the address (&) or the sizeof the object), C evaluation
- calls for the value of an object. The value of most objects is the
- value stored in them, but the value of an array object is actually
- independent of the value stored in the memory location that constitute
- the array: it is instead a pointer to the _first_ of the sequence of
- objects that constitute it. Thus it is completely incorrect to call it
- a `constant pointer to 10 contiguous char datatypes'. What is true,
- however, is that _unless_ it is the operand of & or sizeof (the actual
- situation under discussion), the value of myarray is a `pointer to the
- first of 10 contiguous char objects'. Note that it is very difficult
- to talk of a value being a `constant', after i=5, is &i a constant
- pointer to an int? Is i a constant integer 5?
-
- B: ADDRESS whereas *(myarray + 5) or myarray[5] is the 6th element in
- the array.
-
- Yes. In *(myarray+5) or myarray[5] it is the value of myarray that
- contributes. This indeed is a pointer.
-
- B: The difference between an array and something like "char
- *p=myarray", is that
- B: you can say p++, but you can't say myarray++. You shouldn't say &myarray
-
- The ++ operator needs a `modifiable lvalue' which is a technical term.
- What it means is that the expression must designate an object
- (e.g. (i+j) will not do: that expression designates a value and not an
- object), the object designator must not have a `const' qualified type,
- or if it is a struct, union or array, none of its elements or fields
- may have a const qualified type, (i.e. after `const int i', i++ is
- disallowed), and it shouldn't be an array.
-
- Actually, even if the C standard did not put in the special rule for
- the array, you can easily see it wouldn't have made sense anyway. If
- you wrote myarray = myarray + 1, by the above rules, myarray+1 would be
- a pointer which you would have to assign to an array (if the array
- hadn't yet decayed to a pointer) or to a value (if it had). Neither of
- them makes the slightest sense, so, it is best not to even think of
- these things.
-
- B: either because myarray is a constant, but I read that on some compilers
- B: scanf ignores the dereferencing and does not bother to warn you.
-
- This is where you are most wrong. &myarray is very useful in some
- contexts. It is a pointer to the entire array, and such pointers have
- their uses. You are right in pointing out that passing them to scanf
- invokes undefined behaviour.
-
- B: Hope this helps.
-
- Incorrect statements rarely help. Please read the FAQ, it is an
- excellent source of information about C, and the product of a
- considerable effort on the part of Steve Summit (and others).
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-